package JVM;

import java.util.Random;
import java.util.concurrent.CountDownLatch;

public class CountDownLatchTest {
    static class PreTask implements Runnable{
        private String task;
        private CountDownLatch countDownLatch;

        public PreTask(String task,CountDownLatch countDownLatch){
            this.task=task;
            this.countDownLatch=countDownLatch;
        }

        @Override
        public void run() {
            Random random=new Random();
            try {
                Thread.sleep(random.nextInt(1000)+1000);
                System.out.println(task+"--完成！");
                countDownLatch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        CountDownLatch countDownLatch=new CountDownLatch(3);
        new Thread(()->{
            System.out.println("等待中--");
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("结束！");
        }).start();
        new Thread(new PreTask("TAKS1",countDownLatch)).start();
        new Thread(new PreTask("TAKS2",countDownLatch)).start();
        new Thread(new PreTask("TAKS3",countDownLatch)).start();
    }
}
